package cn.com.cetccst.appstore.jssdk;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import cn.com.cetccst.appstore.log.AppStoreLogger;
import cn.com.cetccst.appstore.tools.Utils;
import cn.com.cetccst.sdk.modelbase.BaseResp;


public class ActionGetContextParams extends BaseJSAction<Map<String, String>, BaseJSAction.Resp> {
    public static final String ACTION = "getContextParams";

    public static int JSSDK_VERSION_CODE = 1;
    private String contextParms;

    ActionGetContextParams(Context context, CXApiImplInnerApp icxapi, String parameters) {
        super(context, icxapi, parameters);
        contextParms = parameters;
    }

    @Override
    public void handleAction(Response response) {
        response.setRespJson(buildSuccessResp(null));
    }

    @Override
    protected Map<String, String> parserParameters(String para) {
        return Utils.getGson().fromJson(para, Map.class);
    }

    @Override
    protected String buildSuccessResp(Resp icxResp) {
        try {
            AppStoreLogger.info( TAG, contextParms);
            JSONObject object = new JSONObject();
            object.put("code", 0);
            object.put("data", getData());
            return object.toString();
        } catch (JSONException e) {
            AppStoreLogger.error("ActionGetContextParams error: ", e);
        }
        return buildFailedResp(BaseResp.ErrCode.ERR_WORK_BENCH_ERROR);
    }

    private JSONObject getData() throws JSONException {
        JSONObject object = new JSONObject(contextParms);
        object.put("versionCode", JSSDK_VERSION_CODE);
        return object;
    }
}
